package cn.com.jslh.boarpro.util.template;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.main.view.SelectDialog;
import cn.com.jslh.boarpro.main.view.fragment.AddTemplateFragment;
import cn.com.jslh.boarpro.util.JSONUtil;

/**
 * Created by Administrator on 2017/11/17.
 */

public class SelectTempalte extends AddTempBaseItem implements AdapterInterface {
    @Override
    public String getFlag() {
        return "select";
    }

    @Override
    public int getLayout() {
        return R.layout.add_template_select_item_layout;
    }

    @Override
    public SlimInjector<? extends AddTempBaseItem> getSlimInjetor() {
        SlimInjector<SelectTempalte> inj = new SlimInjector<SelectTempalte>() {
            @Override
            public void onInject(final SelectTempalte data, IViewInjector injector) {
                JSONObject config = new JSONObject();
                JSONArray matterarry = null;
                try {
                    config = new JSONObject(data.getConfig());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                int isCheck = 0; // 0 不需检测 1检测
                String searchAction = "";
                try {
                    searchAction = config.getString("searchAction");
                    isCheck = config.getInt("isCheck");
                    matterarry = config.getJSONArray("matterarry");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                final JSONObject finalConfig = config;
                final String finalSearchAction = searchAction;
                final JSONArray finalMatterarry = matterarry;
                injector.text(R.id.titleText, data.getTitle())
                        .with(R.id.valueText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                TextView textView = (TextView) view;
                                String placeholder = data.getPlaceholder();
                                textView.setText(placeholder);
                                JSONUtil.configValueTextView(textView, finalConfig);
                            }
                        })
                        .with(R.id.titleText, new IViewInjector.Action() {
                            @Override
                            public void action(View view) {
                                JSONUtil.configTextView((TextView) view, finalConfig);
                            }
                        })
                        .clicked(R.id.valueText, new View.OnClickListener() {
                    @Override
                    public void onClick(final View v) {
                        final String[] datas = new String[finalMatterarry.length()];
                        for (int i = 0; i < finalMatterarry.length(); i++) {
                            try {
                                JSONObject jsonObject = finalMatterarry.getJSONObject(i);
                                datas[i] = jsonObject.getString("name");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        new AlertDialog.Builder(v.getContext())
                                .setTitle(data.getTitle())
                                .setItems(datas, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        try {
                                            data.setResult(finalMatterarry.getJSONObject(which).getString("id"));
                                            data.setPlaceholder(datas[which]);
                                            TemplateHelper.updataItems(v.getContext(), data);
                                            AddTemplateFragment.notifyDatas();
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                })
                                .show();
                    }
                });
            }
        };
        return inj;
    }
}
